Docker & R

Andrew Ba Tran

5/17/2021

What’s the point of an R server?

You can host:

Shiny apps

shiny_apps

R Notebooks

helicopters

Learnr tutorials

Make your own code acadamy type of courses.

learnr

Rstudio Cloud

Spend a few dollars to spin up a server with 32 cores and 192 GB of ram for a few hours for complex data analysis.

rstudio_cloud

API with Plumber

plumber

Host models

Washington Post Python data munging + R modeling + Plumber API = Live election predictions

model

Host models

Washington Post Python data munging + R modeling + Plumber API = Live election predictions

election

What are your options?

  1. Hosting Rmarkdown for free: Github Pages
  2. Hosting Shiny apps (not free): shinyapps.io

shinyio

Host it yourself

Digital Ocean for Personal Use and AWS at Work

Digital Ocean

AWS (or whatever your news org uses)

  • Engineering experts to help
  • Can host internally

Steps for setting up an R server on Digital Ocean

  • Create a Droplet for your project
  • Choose an image wiith at least 1GB of ram
  • Choose a plan
  • Add block storage
  • Choose data center
  • Select SSH keys for authentication
  • Choose a hostname for your droplet
  • Assign a floating IP address
  • Point your domain to the floating IP address

More specific instructions here

Steps for setting up an R server on Digital Ocean

  • Access Droplet via Command Line
    • Download, install, and run the PuTTY SSH client
    • Configure PuTTY session with proper SSH credentials
    • Log into droplet
  • More SysAdmin tasks
    • Set up non-root user with ssh login that has sudo privileges
    • Set up SSL certificates
    • Write the configuration Caddyfile for ports, prooxies, websockets, etc)

Steps for setting up an R server on Digital Ocean

  • More SysAdmin tasks to install R
    • from the command line, download and install R

$ sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

$ sudo apt update

$ sudo apt install r-base

More specific instructions here

Move data over and R scripts

Create/Organize your folders through the command line

Copy a local file to your server:

$ scp depends.R remote_username@10.10.0.2:/remote/directory $ Rscript depends.R

shinyio

Set up the server based on what you want

Shiny server

  • Install Nginx
  • Install the shiny packages
  • Set up appropriate ports to listen/forward

Rstudio cloud

  • Install the Rstudio package
  • Create users for logging in to the service

Simplifying the process with Docker

Brewing and drinking beer

  1. Dockerfile - Describes the steps needed to create an environment. This is the recipe.
  2. Image - When you execute the steps in a Dockerfile, you build the Dockerfile into an image which contains the environment you described. This is the batch of beer.
  3. Registry - Stores built images, so that others can use them. This is akin to a liquor store.
  4. Container - At a specific moment, you can start a container from the image, which amounts to running a process in the built environment. This is drinking a pint from the batch of beer.

Simplifying the process with Docker

  1. You can easily pour many “replicas” of the same beer.
  2. The bartender (a server, in computer terms), is decoupled from the beer we want - we don’t have to go to the brewer and brew a new beer each time we want a pint. 3.As a result, the same bartender can offer many different types of beers

docker

Layers in a Docker Container

  1. Base Operating System
  2. System Dependencies
  3. R
  4. R Packages
  5. Code
  6. Data

Base Operating System

FROM rocker/shiny

System Dependencies

docker_sys ## The directory

files

R packages

WORKDIR /srv/shiny-server

COPY depends.R /srv/shiny-server/
RUN Rscript depends.R

Code and data

docker_code

docker-compose.yml

docker_yml

Run Docker locally

  1. Install Docker
  2. Navigate to folder
  3. Build out project
docker build -t docker_sample_project .

Run Docker locally

  1. First time building will take a while

packages

Host docker locally

Last line in my dockerfile:

EXPOSE 3838

In the command line:

docker-compose down && docker-compose rm && docker-compose up --force-recreate --build

Host docker locally

docker_browse